package com.hc.dao;

import com.hc.entity.Permission;
import com.hc.entity.Role;
import com.hc.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Arrays;

@Slf4j
@Repository
public class UserDao {

    private Role admin = new Role("ADMIN", "管理员");
    private Role user = new Role("USER", "普通用户");

    {
        Permission p1 = new Permission("个人中心", "UserIndex", "/toUserCenter");
        Permission p2 = new Permission("图书列表", "BookList", "/toBookList");
        Permission p3 = new Permission("图书详情", "BookDetail", "/toBookDetail");

        admin.setPermissionList(Arrays.asList(p1, p2, p3));
        user.setPermissionList(Arrays.asList(p1, p2));
    }

    public User selectByName(String username) {
        User user = null;
        log.info("模拟从数据库中查询用户");
        if ("zhangsan".equals(username)) {
            user = new User("zhangsan", "$2a$10$aR5LVhxrsX/Myizd8x6N..tYzL/8cZQFsXmDIwVBuZSBVxp9tiv/u");
            user.setRoleList(Arrays.asList(admin, this.user));
            return user;
        } else if ("lisi".equals(username)) {
            user = new User("lisi", "$2a$10$aR5LVhxrsX/Myizd8x6N..tYzL/8cZQFsXmDIwVBuZSBVxp9tiv/u");
            user.setRoleList(Arrays.asList(this.user));
            return user;
        }
        return user;
    }

}
